/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.sql.ResultSet;
import java.sql.Statement;

import blueprint4j.utils.BindException;
import blueprint4j.utils.BindStringSelectInterface;
import blueprint4j.utils.Log;
import blueprint4j.utils.VectorString;


/**
 * This will offer select options from a select in the database
 */
public class FieldStringSelectDB extends FieldString implements BindStringSelectInterface{
	private static final long serialVersionUID = 1L;

	private String sql_select = null,display_field=null,options_field=null;
    private VectorString options = new VectorString(),display = new VectorString();
    
    /**
     * Build a sql select
     * The display values will come from
     */
    public FieldStringSelectDB( String p_name, int p_field_type ,String p_description,int length,String sql_select,String display_field,String options_field,Entity entity ) {
        super( p_name,p_field_type,p_description,length,entity );
        this.sql_select = sql_select;
        this.display_field =display_field;
        this.options_field = options_field;
    }
    
    public Field newInstance(Entity entity) {
        return new FieldStringSelectDB(getName(),getFieldType(),getDescription(),getLength(),sql_select,display_field,options_field,entity);
    }
    
    public String getSqlSelect() {
		return sql_select;
	}

	public void setSqlSelect(String sql_select) {
		this.sql_select = sql_select;
	}

	/**
     * This build the sql select and populates the values
     */
    public void refresh()
    throws BindException {
        try {
            Statement smt = getEntity().getConnection().createStatement();
            options = new VectorString();
            display = new VectorString();
            for (ResultSet rs = smt.executeQuery(sql_select);rs.next();) {
                options.add(rs.getString(options_field));
                display.add(rs.getString(display_field));
            }
            smt.close();
        } catch (Exception exception) {
            throw new BindException(exception);
        }
    }
    
    public void set(String value){
        if (value != null && !options.contains(value)) {
            Log.trace.out("FieldStringSelectDB set to a value ["+value+"] which is not conatined in the options list ["+options+"]","");
        }
        setObject(value);
    }
    
    public String[] getOptions() {
        return options.toArray();
    }
    
    public String[] getDisplays() {
        return display.toArray();
    }
    
    public String getDisplay() {
        if (get() != null && display.size() > options.indexOf(get())) {
            return display.get(options.indexOf(get()));
        }
        return null;
    }
    
}
